package com.webull.information.center.mam;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

public class GenSplanSql {

	private static String CN_HK_STOCKS = "913256409,913322495,913243631,913425742,913245742,913252981,913322526,913322338,913257153,913322521,913257587,913322440,913252379,913256644,913323339,913253074,913257141,913252773,913257022,913257401,913253191,913253232,913256262,925177987,913252947,913322221,913322321,913253116,913322531,913257617,913257626,913256710,913257476,913322511,925167208,913253061,913253274,913322306,913322535,913322418,913256738,913256636,913253259,913322516,913322373,913253300,913253007,913253164,913323352,913252780,913253124,913257120,913257461,913252914,913257020,913245895,913252924,913256283,913323361,913256378,913253035,913244134,913322317,913252641,913322268,913252950,913253122,913256915,913253163,913253126,913252721,913253098,913322525,913323343,913246847,913256313,913322524,913322519,913253222,913244705,913256507,913322240,913322386,913252395,913252470,913256719,913322382,913253197,913257428,913322467,913257635,913257610,913252595,913253248,913257516,913301873,913252925,913253022,913322318,913322437,913253308,913322522,913322534,913322244,913257549,913322277,913246928,913252915,913256351,913257295,913322217,913322517,913243628,913252431,913252536,913323365,913252962,913243755,913257601,913244693,913322363,913322508,913323354,913256721,913257631,913253047,913244718,913322465,913256522,913256717,913256766,913253078,913253105,913253313,913253118,913253209,913244514,913246874,913246041,913322463,913253091,913256265,913247021,913253000,913257055,913257318,925163609,913253054,913257119,913253226,913253227,913247006,913256278,913246574,913256503,913245569,913322448,913322509,913253290,913252628,913257365,913257608,913256745,913257160,913257447,913253190,913253302,913256491,913252554,913252942,913256436,913256573,913253121,913253183,913244717,913257007,913322507,913253187,913252616,913244853,913323356,913256508,913322532,913246467,913245847,913257250,913252478,913246815,913252349,913244615,913256309,913256826,913322234,913322243,913322276,913322361,913253231,913256381,913253004,913253066,913253312,913256463,913253154,913322248,913252659,913253321,913256520,913246257,913246975,913252941,913244687,913244822,925329054,913244659,913257606,913253225,913253272,913246837,913252403,913252582,913246953,913252336,913252612,913252935,913253080,913253112,913253241,913252436,913252437,913322216,913322515,913257366,913322313,913256553,913256724,913257042,913257205,913257251,913257539,913253082,913253271,913252719,913322529,913323346,913430046,913256544,913257339,913253045,913253315,913243718,913322302,913322417,913322260,913256943,913252514,913253094,913253114,913244695,913252662,913252740,913252793,913252998,913253040,913252561,913256316,913256757,913257509,913257623,913322238,913322304,925165945,925341830,913256502,913256621,913256850,913246919,913252559,913252622,913252747,913246055,913246463,913246742,913252344,913245253,913253038,913253075,913256557,913256688,913256964,913257308,913257540";
	private static String USA_STOCKS = "";// 美国股票
	private static String IN_STOCKS = "";// 印度
	static {
		try {
			USA_STOCKS = FileUtils.readFileToString(new File(
					"C:/ws4/webull-information/webull-information-center/src/test/java/com/webull/information/center/mam/us_stocks.txt"),
					"utf-8");
			IN_STOCKS = FileUtils.readFileToString(new File(
					"C:/ws4/webull-information/webull-information-center/src/test/java/com/webull/information/center/mam/us_stocks.txt"),
					"utf-8");
		} catch (IOException e) {
		}
	}
	public static void main(String[] args) {

		GenSplanSql ss = new GenSplanSql();

		{
			String[] split = StringUtils.split(CN_HK_STOCKS, ",");
		Set<String> cnhkSTOCKS = new HashSet<>();
		for (String ssss0 : split) // 除重
			cnhkSTOCKS.add(ssss0.trim());
			// cnhkSTOCKS.forEach(v0 -> ss.genSinaCn_1(v0));
		}

		{
		String[] split2 = StringUtils.split(USA_STOCKS, ",");
		Set<String> usaSTOCKS = new HashSet<>();
		for (String ssss0 : split2) // 除重
			usaSTOCKS.add(ssss0.trim());

			// usaSTOCKS.forEach(v0 -> ss.genSinaCn_1(v0));

			// usaSTOCKS.forEach(v0 -> ss.genYahoo_1(v0));
			// usaSTOCKS.forEach(v0 -> ss.genCnbc_1(v0, "2"));
			usaSTOCKS.forEach(v0 -> ss.genBloomb_1(v0, "US"));
		}

		{
			String[] split3 = StringUtils.split(IN_STOCKS, ",");
		Set<String> inSTOCKS = new HashSet<>();
		for (String ssss0 : split3) // 除重
			inSTOCKS.add(ssss0.trim());
			// inSTOCKS.forEach(v0 -> ss.genMctrol_1(v0));
		}



	}


	private void genSinaCn_1(String tickerId) {
		if (!("" + tickerId).matches("\\d+"))
			return;

		Splan sp = new Splan();
		sp.setTarget(tickerId);
		sp.setBranch("cn");
		sp.setSource("sinaCn");
		sp.setMode(1);
		sp.setCron(StringUtils.replace("0 $ * * * ?", "$", "" + RandomUtils.nextInt(0, 60)));
		System.out.println(genSplanSql(sp));
	}

	private void genMctrol_1(String tickerId) {
		if (!("" + tickerId).matches("\\d+"))
			return;

		Splan sp = new Splan();
		sp.setTarget(tickerId);
		sp.setBranch("us");
		sp.setSource("mCtrol");
		sp.setMode(1);
		sp.setCron(StringUtils.replace("0 $ * * * ?", "$", "" + RandomUtils.nextInt(0, 60)));
		System.out.println(genSplanSql(sp));
	}

	private void genYahoo_1(String tickerId) {
		if (!("" + tickerId).matches("\\d+"))
			return;

		Splan sp = new Splan();
		sp.setTarget(tickerId);
		sp.setBranch("us");
		sp.setSource("yahoo");
		sp.setMode(1);
		sp.setCron(StringUtils.replace("0 $ * * * ?", "$", "" + RandomUtils.nextInt(0, 60)));
		System.out.println(genSplanSql(sp));
	}

	private void genCnbc_1(String tickerId, String ext) {
		if (!("" + tickerId).matches("\\d+"))
			return;

		Splan sp = new Splan();
		sp.setTarget(tickerId);
		sp.setBranch("us");
		sp.setSource("cnbc");
		sp.setTarget_ext(ext);
		sp.setMode(1);
		sp.setCron(StringUtils.replace("0 $ * * * ?", "$", "" + RandomUtils.nextInt(0, 60)));
		System.out.println(genSplanSql(sp));
	}

	// add(BloomTag.produce(1, "CH"));
	// add(BloomTag.produce(2, "HK"));
	// add(BloomTag.produce(5, "JP"));// 日本
	// add(BloomTag.produce(6, "US"));
	// add(BloomTag.produce(12, "IN"));// 印度
	// add(BloomTag.produce(13, "SP"));// 新加坡

	private void genBloomb_1(String tickerId, String ext) {
		if (!("" + tickerId).matches("\\d+"))
			return;

		Splan sp = new Splan();
		sp.setTarget(tickerId);
		sp.setBranch("us");
		sp.setSource("bloomb");
		sp.setTarget_ext(ext);
		sp.setMode(1);
		sp.setCron(StringUtils.replace("0 $ * * * ?", "$", "" + RandomUtils.nextInt(0, 60)));
		System.out.println(genSplanSql(sp));
	}

	private static final String Sql0 = "INSERT INTO `wl_information`.`wli_specl_news_plan` (`target`, `target_ext`, `source`, `mode`, `cron`, `branch`,`remark`) "
			+ " VALUES ({target}, {target_ext}, {source}, {mode}, {cron}, {branch}, {remark});";

	private String genSplanSql(Splan sp) {
		String sql = Sql0;
		if (StringUtils.isNotBlank(sp.getTarget()))
			sql = StringUtils.replaceOnce(sql, "{target}", "'" + sp.getTarget() + "'");
		if (StringUtils.isNotBlank(sp.getTarget_ext()))
			sql = StringUtils.replaceOnce(sql, "{target_ext}", "'" + sp.getTarget_ext() + "'");
		if (StringUtils.isNotBlank(sp.getSource()))
			sql = StringUtils.replaceOnce(sql, "{source}", "'" + sp.getSource() + "'");
		if (sp.getMode() != null)
			sql = StringUtils.replaceOnce(sql, "{mode}", sp.getMode() + "");
		if (StringUtils.isNotBlank(sp.getCron()))
			sql = StringUtils.replaceOnce(sql, "{cron}", "'" + sp.getCron() + "'");

		if (StringUtils.isNotBlank(sp.getBranch()))
			sql = StringUtils.replaceOnce(sql, "{branch}", "'" + sp.getBranch() + "'");
		if (StringUtils.isNotBlank(sp.getRemark()))
			sql = StringUtils.replaceOnce(sql, "{remark}", "'" + sp.getRemark() + "'");
		sql = StringUtils.replacePattern(sql, "\\{[^}]+\\}", "null");
		return sql;
	}
	private static class Splan {
		private String target;
		private String target_ext;
		private String source;
		private Integer mode;
		private String cron;
		private String branch;
		private String remark;



		private String getTarget() {
			return target;
		}

		private void setTarget(String target) {
			this.target = target;
		}

		private String getTarget_ext() {
			return target_ext;
		}

		private void setTarget_ext(String target_ext) {
			this.target_ext = target_ext;
		}

		private String getSource() {
			return source;
		}

		private void setSource(String source) {
			this.source = source;
		}

		private Integer getMode() {
			return mode;
		}

		private void setMode(Integer mode) {
			this.mode = mode;
		}

		private String getCron() {
			return cron;
		}

		private void setCron(String cron) {
			this.cron = cron;
		}

		private String getBranch() {
			return branch;
		}

		private void setBranch(String branch) {
			this.branch = branch;
		}

		private String getRemark() {
			return remark;
		}

		private void setRemark(String remark) {
			this.remark = remark;
		}


	}
}
